class Utils {
    //字符串格式化,将 format('xxxx{}xx{}xx','A','B')  = xxxxAxxBxx
    public format(str: string, ...args: string[]): string {
        if (args == null || args.length == 0) {
            return str;
        }
        args.forEach(arg => {
            str = str.replace('{}', arg);
        })
        return str;
    }

    //复制文本到剪切板
    public toClipboard(txt:string) {
        const handleCopy = (e: ClipboardEvent) => {
            // clipboardData 可能是 null
            e.clipboardData && e.clipboardData.setData('text/plain', txt);
            e.preventDefault();
            // removeEventListener 要传入第二个参数
            document.removeEventListener('copy', handleCopy);
        };
        document.addEventListener('copy', handleCopy);
        document.execCommand('copy');
    }

    public downloadFile(url:string, fileName:string) {
        const a = document.createElement('a')
        a.href = url
        a.download = fileName // 下载后文件名
        a.style.display = 'none'
        document.body.appendChild(a)
        a.click() // 点击下载
        document.body.removeChild(a) // 下载完成移除元素
    }

}

export default new Utils();